tm.air = {
	moreLnk: '#frm-air .search_controls .more-less-controls .more',
	lessLnk: '#frm-air .search_controls .more-less-controls .less',
	airTabs: {
		oneWay: '#one-way',
		roundTrip: '#round-trip',
		severalFlights: '#several-flights'
	},
    init: function()
    {
		var
			aId = $('#frm-air-header .air-tabs li.active a').attr('id')
		;
		
		$('div.flights div.flight-item').hide();
		$('div.flights div.flight-item.' + aId).show();
		
		if ($('#several-flights').parents('li').hasClass('active')) {
			var
				size = $('#frm-air div.flights div.several-flights div.flight').size(),
				sizeVisible = $('#frm-air div.flights div.several-flights div.flight:visible').size()
			;
			if (sizeVisible == size) {
				$('#add-new-flight a').addClass('disabled');
			}
			$('#add-new-flight').show();
		}
		$('div.several-flights .flight .remove-flight a').bind('click', tm.air.removeFlightFn);
		
		$('div.search_controls .more-less-controls .less, .search_controls .more-less-controls .more').unbind('click');
		$(tm.air.lessLnk).hide().bind('click', tm.air.lessLnkClick);
		$(tm.air.moreLnk).show().bind('click', tm.air.moreLnkClick);
		
		if ($('#frm-air').length > 0) {
			if (
				$('#tourists-number').val() > 1 ||
				$('#flight-class input[type="hidden"]').val() != 'ECONOMY' ||
				$('input.currency[name="pf"]').val() != '' ||
				$('input.currency[name="pt"]').val() != '' ||
				$('#direct-flight').is(':checked') ||
				$('#air-use-markup').is(':checked') ||
				$('#flexible-dates').is(':checked')
			) {
				$('#cnt-top .char-chief .tip .tcnt').width(160);
				$('#search-more-text').remove();
				$(tm.air.moreLnk).hide();
				$(tm.air.lessLnk).css('display', 'block');
				$('#frm-air .extended-search').show();
			}
		}

		$('ul.air-tabs li a').bind('click', tm.air.airTabClick);
		
		$('#add-new-flight a').bind('click', {restore: false}, tm.air.addNewFlightFn);

		tm.air.autocompleteHandle($('.from_to_airport'));
		
		$('#frm-air').submit(tm.air.searchFormSubmit);
		
		for (var p = 0; p < 3; p++) {
			tm.air.inputsAirports[p].af.code = $('#from_airport_' + p + '_code').val();
			tm.air.inputsAirports[p].af.text = $('div.flight:visible input.from_airport_' + p).val() || '';
			tm.air.inputsAirports[p].at.code = $('#to_airport_' + p + '_code').val();
			tm.air.inputsAirports[p].at.text = $('div.flight:visible input.to_airport_' + p).val() || '';
		}
		
		tm.air.setFocusAtFirstField();
    },
    setFocusAtFirstField: function()
    {
    	$('div.flights .flight-item:visible input.from_to_airport:first').focus();
    },
    thisVal: null,
    timeout: null,
    
    inputsAirports: [
		{
			'af': {'code': '', 'text': ''},
	    	'at': {'code': '', 'text': ''}
		},
		{
			'af': {'code': '', 'text': ''},
			'at': {'code': '', 'text': ''}
		},
		{
			'af': {'code': '', 'text': ''},
			'at': {'code': '', 'text': ''}
		}
	],
    autocompleteHandle: function(elm)
    {
    	elm.bind('keyup', tm.air.ac_keyup);
    	elm.bind('keydown', tm.air.ac_keydown);
       	elm.bind('contextmenu', function(e) {
       		e = e || window.event;
       		e.cancelBubble = true;
       		return false;
       	});
    },
    firstKeyDown: true,
    acTimer: null,
    acTimeout: 1500,
    ac_keyup: function(e) {
    	var
    		elm = $(this),
    		reg = /^[а-яА-Я\w\s\-\,()]+$/gi
    	;
    	
    	if (! elm.val().match(reg) && $.trim(elm.val()).length > 1) {
    		tm.air.remove_AC_List();
    		tm.air.unset_AC_error(elm);
    		tm.air.set_AC_error(elm, 'TYPE_CORRECT_AIRPORT_NAME'.translate());
    		return false;
    	}
    },
    ac_keydown: function(e) {
    	var
    		elm = $(this),
    		key = e.keyCode,
    		num = parseInt(String(elm.attr('class').match(/_airport_./)).replace('_airport_', ''))
    	;
    	tm.air.unset_AC_error(elm);
    	if (
    		key == 8 ||
    		key == 32 ||
    		key == 45 ||
    		key == 46 ||
    		(key >= 48 && key <= 90) ||
    		(key >= 96 && key <= 105) ||
    		key == 109 ||
    		(key >= 188 && key <= 192) ||
    		key == 219 ||
    		key == 221 ||
    		key == 222
    	) {
    		if (tm.air.firstKeyDown) {
	    		tm.air.firstKeyDown = false;
	    		clearTimeout(tm.air.acTimer);
	    		tm.air.acTimer = setTimeout(function() {
	        		tm.air.thisVal = elm.val();
	            	if (tm.air.thisVal.length > 1 && $.trim(tm.air.thisVal) != '') {
	            		tm.air.removeAcWait(elm);
	            		tm.air.acWait(elm);   //*** Крутилко ***
	            		$.ajax({
		            		url: '/a_search/air.getAirports',
		            		type: 'POST',
		            		dataType: 'json',
		            		data: {id: elm.attr('class').replace('from_to_airport', '').replace('waiting', ''), limit: 20, q: tm.air.thisVal},
		            		success: function(data) {
		            			tm.air.removeAcWait(elm);   //*** Крутилко ***
		            			var setError = false;
		            			if (data && data.info.length > 0) {
		            				tm.air.unset_AC_error(elm);
		            				tm.air.remove_AC_List();
			            			tm.air.create_AC_List(elm, data);
		            			} else {
		            				if (elm.attr('class').match('from_airport_')) {
		            					if ($('#from_airport_' + num + '_code').val() != tm.air.inputsAirports[num].af.code || elm.val() != tm.air.inputsAirports[num].af.text) {
		            						setError = true;
		            					}
		            				} else if (elm.attr('class').match('to_airport_')) {
		            					if ($('#to_airport_' + num + '_code').val() != tm.air.inputsAirports[num].at.code || elm.val() != tm.air.inputsAirports[num].at.text) {
		            						setError = true;
		            					}
		            				}
		            				if (setError) {
		            					tm.air.set_AC_error(elm, 'TYPE_CORRECT_AIRPORT_NAME'.translate());
		            				} else {
		            					tm.air.unset_AC_error(elm);
		            				}
		            			}
		            			tm.air.firstKeyDown = true;
		            		},
		            		error: function() {
		            			tm.air.removeAcWait(elm);   //*** Крутилко ***
		            		}
		            	});
	            	}
	        	}, tm.air.acTimeout);
	    		setTimeout(function() {
	    			tm.air.firstKeyDown = true;
	    		}, tm.air.acTimeout);
	    	}
    	} else if (key == 27) {
    		tm.air.remove_AC_List();
    		e.stopPropagation();
    		e.preventDefault();
    	} else if (key == 40) {
    		// Down
    		tm.air.moveSelect(1);
    	} else if (key == 38) {
    		// Up
    		tm.air.moveSelect(-1);
    	} else if (key == 13) {
    		if ($('#ac_results').is(':visible')) {
	    		$('#ac_results li.hovered a').trigger('click');
	    		return false;
    		}
    	} /*else if (key != 9 && key != 16 && key != 17 && key != 18 && key != 35 && key != 36 && key != 37 && key != 39 && key != 91) {
    		tm.air.set_AC_error(elm, 'TYPE_CORRECT_AIRPORT_NAME'.translate());
    	}*/

    },
    acWait: function(elm)
    {
    	elm.addClass('waiting');
    	//$('#ac_results').remove();
    },
    removeAcWait: function(elm)
    {
    	elm.removeClass('waiting');
    },/*                                        *** Крутилко ***        */
    active: -1,
    moveSelect: function(step) {
		var
			list = $('#ac_results'),
			listItems = $('#ac_results li')
		;
		$('#ac_results li.hovered').removeClass();
    	//listItems.slice(tm.air.active, tm.air.active + 1).removeClass();
		tm.air.movePosition(step);
        var activeItem = listItems.slice(tm.air.active, tm.air.active + 1).addClass('hovered');
        var offset = 0;
        listItems.slice(0, tm.air.active).each(function() {
			offset += this.offsetHeight;
		});
        if((offset + activeItem[0].offsetHeight - list.scrollTop()) > list[0].clientHeight) {
            list.scrollTop(offset + activeItem[0].offsetHeight - list.innerHeight());
        } else if(offset < list.scrollTop()) {
            list.scrollTop(offset);
        }
	},
    movePosition: function(step) {
		var
			list = $('#ac_results'),
			listItems = $('#ac_results li')
		;
		
		tm.air.active += step;
		if (tm.air.active < 0) {
			tm.air.active = listItems.size() - 1;
		} 
		else if (tm.air.active >= listItems.size()) {
			tm.air.active = 0;
		}
	},
    create_AC_List: function(elm, data)
    {
    	var liData = '';
    	for(var i = 0; i < data.info.length; i++) {
    		evenOdd = (i%2 == 0) ? 'even' : 'odd';
    		setCity = (data.info[i].city != '') ? ' <span class="city">' + data.info[i].city + '</span>,' : '';
    		setCode = (data.info[i].code != undefined && data.info[i].code != '') ? '<span class="code"> (' + data.info[i].code + ')</span>' : '';
    		liData += '<li><a class="' + evenOdd + '" href="javascript:void(0)" name="' + data.info[i].code + '" targetid="' + data.id + '"><span class="airport"><span class="a-name">' + data.info[i].airport + '</span>' + setCode + '</span>' + setCity + ' <span class="country">' + data.info[i].country + '</span></a></li>';
    	}
    	var acResults = '<div id="ac_results"><ul>' + liData + '</ul></div>';
    	var offset = elm.offset();
    	$('body').append(acResults);
    	$('#ac_results').css({
    		left: offset.left + 'px',
    		top: offset.top + elm.height() + parseInt(elm.css('paddingTop')) + parseInt(elm.css('paddingBottom')) + parseInt(elm.css('borderTopWidth')) + parseInt(elm.css('borderBottomWidth')) + 'px',
    		//width: elm.width() - parseInt($(acResults).css('paddingLeft')) - parseInt($(acResults).css('paddingRight')) + parseInt(elm.css('paddingLeft')) + parseInt(elm.css('paddingRight')) + 200 + 'px',
    		width: 620, /* for chrome */
    		maxHeight: $('#ac_results li').height() * 10 + 'px'
    	});
    	$('#ac_results li').unbind('mouseover').bind('mouseover', function() {
    		$('#ac_results li').removeClass();
    		$(this).addClass('hovered');
    		tm.air.active = $('#ac_results li').index(this);
    	}).unbind('mouseout').bind('mouseout', function() {
    		$(this).removeClass('hovered');
    	});
    	$('#ac_results a').bind('click', {input: elm}, tm.air.selectFromAC);
    },
    remove_AC_List: function()
    {
    	$('#ac_results').remove();
    },
    selectFromAC: function(e)
    {
    	e = e || window.e;
    	var
    		$elm = $(this),
    		input = e.data.input,
    		code = $elm.attr('name')
    	;
    	input.val($elm.find('.city').text() + ', ' + $elm.find('.country').text() + ', ' + $elm.find('.airport .a-name').text() + ' ' + $elm.find('.airport .code').text().toUpperCase());
    	$('#' + $elm.attr('targetid') + '_code').val(code);
    	
    	//var num = input.attr('class').match('_airport_').replace('_airport_', '').replace('from_to_airport', '');
    	var num = input.attr('class').match(/_airport_./i);
    	num = parseInt(String(num).replace('_airport_', ''));
    	
    	if (input.attr('class').match('from_airport_')) {
	    	tm.air.inputsAirports[num].af.code = code;
	    	tm.air.inputsAirports[num].af.text = input.val()/*.replace(/\s+/g, ' ')*/;
    	} else if (input.attr('class').match('to_airport_')) {
    		tm.air.inputsAirports[num].at.code = code;
	    	tm.air.inputsAirports[num].at.text = input.val()/*.replace(/\s+/g, ' ')*/;
    	}
    	
    	$('#ac_results').fadeOut('fast', function() {
    		if (input.attr('class').match('from_airport_')) {
    			input.parents('.flight-row').next(0).find('input.from_to_airport').select();
    		}
    	});
    	tm.air.active = -1;
    },
    baloonTimer: null,
    set_AC_error: function(elm, baloonText)
    {
    	elm.css({
    		color: '#ff0000'
    	}).focus();
    	$('#ac_results').fadeOut('fast');
    	tm.air.showBaloon(elm, baloonText);
    },
    showBaloon: function(elm, baloonText)
    {
    	tm.air.hideBaloon();
    	$('body').append('<div id="error-baloon"><div class="t"></div><div class="m">' + baloonText + '</div><div class="b"></div></div>');
    	var
    		offset = elm.offset(),
    		errorBaloon = $('#error-baloon')
    	;
    	errorBaloon.css({
    		left: offset.left + elm.width() - 10 + 'px',
    		top: offset.top - errorBaloon.height() + elm.height() / 2 + 'px'
    	});
    	tm.air.baloonTimer = setTimeout(function() {
    		errorBaloon.fadeOut(function() {
    			$(this).remove();
    		});
    	}, 5000);
    },
    hideBaloon: function()
    {
    	$('#error-baloon').remove();
    },
    unset_AC_error: function(elm)
    {
		window.clearTimeout(tm.air.baloonTimer);
		$(elm).css({
    		color: '#666'
    	});
    	tm.air.hideBaloon();
    },
    lessLnkClick: function()
    {
    	$(tm.air.lessLnk).hide();
		$('#frm-air .extended-search').hide();
		$(tm.air.moreLnk).show();
    },
    moreLnkClick: function()
    {
    	$(tm.air.moreLnk).hide();
		$('#frm-air .extended-search').show();
		$(tm.air.lessLnk).show();
    },
    airTabClick: function()
    {
    	var
    		$this = $(this),
    		flight = $('div.flights div.flight-item'),
    		addNewFlight = $('#add-new-flight'),
    		flHolder = $('#flights-holder'),
    		
    		thisId = $('.air-tabs li.active a').attr('id'),
    		clickedId = $this.attr('id'),
    		controlsContainer = $('.flights .' + thisId + ' .flight:first'),
    		thisControls = {},
    		time1Input = $('.flights .' + clickedId + ' input.time1')
    	;
    	tm.air.remove_AC_List();
    	tm.air.hideBaloon();
    	
    	thisControls['thisAF'] = controlsContainer.find('input.from_airport_0').val();
    	thisControls['thisAT'] = controlsContainer.find('input.to_airport_0').val();
    	thisControls['thisDate0'] = controlsContainer.find('input.date0').val();
    	thisControls['thisDate1'] = $('.flights .' + thisId + ' .flight input.date1').val();
    	thisControls['thisTime0'] = controlsContainer.find('input.time0').val() == '' || controlsContainer.find('input.time0').val() == null  || controlsContainer.find('input.time0').val() == undefined ? 0 : parseInt(controlsContainer.find('input.time0').val());
    	thisControls['thisTime1'] = time1Input.val() == '' || time1Input.val() == null  || time1Input.val() == undefined ? 0 : parseInt(time1Input.val());
    	
    	var time1Inp = $('.flights .' + thisId + ' .flight input.time1');
    	if (time1Inp.length > 0) {
	    	var time1Val = time1Inp.val();
	    	thisControls['thisTime1'] = time1Val == '' || time1Val == null  || time1Val == undefined ? 0 : parseInt(time1Val);
    	}

		// ------------------------------------------------------------------------------------------------------------------------------
    	if (thisControls['thisAF'] == tm.air.inputsAirports[0].af.text) {
    		$('.flights .' + clickedId + ' input.from_airport_0').val(thisControls['thisAF']);
    	} else {
    		$('.flights .' + clickedId + ' input.from_airport_0').val('');
    	}
    	if (thisControls['thisAT'] == tm.air.inputsAirports[0].at.text) {
    		$('.flights .' + clickedId + ' input.to_airport_0').val(thisControls['thisAT']);
    	} else {
    		$('.flights .' + clickedId + ' input.to_airport_0').val('');
    	}
    	$('.flights .' + clickedId + ' input.date0').val(thisControls['thisDate0']);
    	$('.flights .' + clickedId + ' input.date1').val(thisControls['thisDate1']);
    	
    	var time0Input = $('.flights .' + clickedId + ' input.time0');
    	time0Input.val(thisControls['thisTime0']);
    	time0Input.siblings('.custom-select-body').find('ul li.active').removeClass('active');
    	time0Input.siblings('.custom-select-body').find('ul li:nth-child(' + (thisControls['thisTime0']+1) + ')').addClass('active');
     	var time0Text = thisControls['thisTime0'] < 10 ? '0' + thisControls['thisTime0'] : thisControls['thisTime0'];
     	time0Input.siblings('.custom-select-header').find('span').text(time0Text + ':00');

    	
     	if (time1Input.length > 0) {
     		time1Input.val(thisControls['thisTime1']);
     		time1Input.siblings('.custom-select-body').find('ul li.active').removeClass('active');
     		time1Input.siblings('.custom-select-body').find('ul li:nth-child(' + (thisControls['thisTime1']+1) + ')').addClass('active');
	     	var time1Text = thisControls['thisTime1'] < 10 ? '0' + thisControls['thisTime1'] : thisControls['thisTime1'];
	     	time1Input.siblings('.custom-select-header').find('span').text(time1Text + ':00');
    	}
     	// ------------------------------------------------------------------------------------------------------------------------------

    	$('.air-tabs li.active').removeClass('active');
    	$this.parent(0).addClass('active');
    	$('#add-new-flight a').removeClass('disabled');
    	flight.hide();
		switch (this.getAttribute('id')) {
			case tm.air.airTabs.oneWay.replace('#', ''):
				$('div.flights div.one-way').show();
				addNewFlight.hide();
				flHolder.remove();
				tm.air.emptyHiddenCodes();
				break;
			case tm.air.airTabs.roundTrip.replace('#', ''):
				$('div.flights div.round-trip').show();
				addNewFlight.hide();
				flHolder.remove();
				tm.air.emptyHiddenCodes();
				break;
			case tm.air.airTabs.severalFlights.replace('#', ''):
				$('div.flights div.several-flights div.flight').not(':first').hide();
				$('div.flights div.several-flights').show();
				addNewFlight.show();
				if (
						thisId == tm.air.airTabs.roundTrip.replace('#', '')
						&&
						(
								$('div.flights div.' + tm.air.airTabs.severalFlights.replace('#', '') + ' input.from_airport_0').val() != ''
								||
								$('div.flights div.' + tm.air.airTabs.severalFlights.replace('#', '') + ' input.to_airport_0').val() != ''
						)
					)
				{
					$('div.flights div.several-flights div.flight:nth-child(2)').show();
					$('div.flights div.several-flights input.from_airport_1').val($('div.flights div.several-flights input.to_airport_0').val());
					$('div.flights div.several-flights input.to_airport_1').val($('div.flights div.several-flights input.from_airport_0').val());
					$('#from_airport_1_code').val($('#to_airport_0_code').val());
					$('#to_airport_1_code').val($('#from_airport_0_code').val());
					tm.air.inputsAirports[1].af.text = tm.air.inputsAirports[0].at.text;
					tm.air.inputsAirports[1].af.code = tm.air.inputsAirports[0].at.code;
					tm.air.inputsAirports[1].at.text = tm.air.inputsAirports[0].af.text;
					tm.air.inputsAirports[1].at.code = tm.air.inputsAirports[0].af.code;
					$('div.flights div.several-flights div.flight:nth-child(2) input.date0').val($('div.flights div.' + tm.air.airTabs.roundTrip.replace('#', '') + ' input.date1').val());
					var secondTime = parseInt($('div.flights div.' + tm.air.airTabs.roundTrip.replace('#', '') + ' input.time1').val());
					var secondTimeText = (secondTime < 10 ? '0' : '') + secondTime + ':00';
					var selectorTempText = 'div.flights div.several-flights div.flight:nth-child(2) .time';
					$(selectorTempText + ' input.time1').val(secondTime);
					$(selectorTempText + ' .custom-select-header span').text(secondTimeText);
					$(selectorTempText + ' .custom-select-body ul li.active').removeClass('active');
					$(selectorTempText + ' .custom-select-body ul li:nth-child(' + (secondTime+1) + ')').addClass('active');
				}
				break;
		}
		tm.air.setFocusAtFirstField();
    },
    emptyHiddenCodes: function()
    {
    	$('input.hidden-airports').each(function() {
			if ($(this).attr('id').match(/[1-2]+/gi)) {
				$(this).val('');
			}
		});
    },
    addNewFlightFn: function(e, r)
    {
    	var
    		restore = (r != undefined && r != null) ? r : e.data.restore,
    		flightsSize = $('div.flights div.several-flights .flight').size(),
    		flightsVisibleSize = $('div.flights div.several-flights .flight:visible').size(),
    		addNewFlightLnk = $('#add-new-flight a')
    	;
    	if ($('#several-flights').parent(0).hasClass('active')) {
    		if (flightsVisibleSize < flightsSize) {
    			var next = $('div.flights div.several-flights .flight:visible').next('.flight:hidden');
    			tm.air.clearFields(next);
    			next.slideDown();
    			if (flightsVisibleSize == flightsSize - 1) {
    				addNewFlightLnk.addClass('disabled');
    			}
    		} else {
    			addNewFlightLnk.addClass('disabled');
    		}
    	} else {
    		addNewFlightLnk.addClass('disabled');
    	}
    },
    clearFields: function(elm)
    {
    	var inp = elm.find('input');
		inp.each(function() {
			var $this = $(this);
			if ($this.metadata().defVal != undefined) {
				$this.val($this.metadata().defVal);
    		} else {
    			$this.val('');
    		}
		});
    },
    removeFlightFn: function()
    {
    	var
    		addNewFlightLnk = $('#add-new-flight a'),
    		flight = $(this).parents('div.flight')
    	;
    	flight.slideUp(function() {
    		tm.air.clearFields($(this));
    	});
    	if (addNewFlightLnk.hasClass('disabled')) {
    		addNewFlightLnk.removeClass('disabled');
    	}
    },
    searchFormSubmit: function(e)
    {
    	var
    		$this = $(this),
    		active = $('#frm-air-header .air-tabs li.active a').attr('id'),
    		afCounter = 0,
    		atCounter = 0,
    		inputSubmitError = false,
    		lastErrorInput = null,
    		isEmpty = false;
    	;
    	
    	$('div.flights div.' + active + ' .flight:visible input.date-picker').each(function(i) {
    		$(this).attr('name', 'dt' + i);
    	});
    	$('div.flights div.' + active + ' .flight:visible .time > input[type="hidden"]').each(function(i) {
    		$(this).attr('name', 'tm' + i);
    	});
    	$('div.flights div.' + active + ' .flight:visible input.from_to_airport').each(function() {
    		var
    			$newthis = $(this),
    			id = $newthis.attr('class').replace('from_to_airport', ''),
    			i = parseInt(String(id.match(/_airport_./gi)).replace('_airport_', ''))
    		;
    		if ($newthis.val() == '' && isEmpty == false) {
    			inputSubmitError = true;
    			lastErrorInput = $newthis;
				isEmpty = true;
    		} else {
	    		if (id.match('from_airport_')) {
	    			var afcode = $('#from_airport_' + afCounter + '_code');
	    			afcode.attr('name', 'af' + afCounter);
	    			afCounter++;
	    			if ($newthis.val() != tm.air.inputsAirports[i].af.text || $('#from_airport_' + i + '_code').val() != tm.air.inputsAirports[i].af.code) {
	//    				alert('thisVal: ' + $newthis.val() + '\nafText: ' + tm.air.inputsAirports[i].af.text + '\nfrom_airport_code: ' + $('#from_airport_' + i + '_code').val() + '\nafCode: ' + tm.air.inputsAirports[i].af.code);
	    				inputSubmitError = true;
	    				lastErrorInput = $('div.flights div.' + active + ' .flight:visible input.from_airport_' + i);
	    			}
	    		} else if (id.match('to_airport_')) {
	    			$('#to_airport_' + atCounter + '_code').attr('name', 'at' + atCounter);
	    			atCounter++;
	    			if ($newthis.val() !== tm.air.inputsAirports[i].at.text || $('#to_airport_' + i + '_code').val() != tm.air.inputsAirports[i].at.code) {
	//    				alert('thisVal: ' + $newthis.val() + '\natText: ' + tm.air.inputsAirports[i].at.text + '\nto_airport_code: ' + $('#to_airport_' + i + '_code').val() + '\natCode: ' + tm.air.inputsAirports[i].at.code);
	    				inputSubmitError = true;
	    				lastErrorInput = $('div.flights div.' + active + ' .flight:visible input.to_airport_' + i);
	    			}
	    		}
    		}
    	});
    	
    	$('#tp').val($this.find('ul.air-tabs li.active a').metadata().type);
    	
    	if (inputSubmitError) {
    		tm.air.unset_AC_error();
    		setTimeout(function() {
    			e.stopPropagation();
        		tm.Popup.popupClose();
        		//tm.air.set_AC_error($('#air_search_btn'), errorText);
        		tm.air.set_AC_error(lastErrorInput, 'TYPE_CORRECT_AIRPORT_NAME'.translate());
    		}, 500);
    		$('input.hidden-airports').removeAttr('name');
    		return false;
    	}
    }
};

$(tm.air.init);